home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2007 December / PCWKCD1207B.iso / Blogowanie poza sfera / Scribefire-1.4.2 / scribefire-1.4.2-fx+fl.xpi / components / nsPerformancingService.js next >
Encoding:
Text File  |  2007-07-24  |  12.9 KB  |  310 lines

  1.  /* 
  2.  Performancing for Firefox
  3.  XPCOM JS Implementation
  4.  ---------------
  5.  
  6.  TODO:
  7.  Slowly move code here to gain performance and modulation
  8.  REMOVE IDL and use wrappedJSObject
  9.  
  10.  See IDL for current features
  11. */
  12.  
  13. //Addon Topics
  14. const PERFORMANCING_ADDON_PFFSTART_TOPIC = "performancing-addon-pffstart-topic";
  15. const PERFORMANCING_ADDON_PFFENABLE_TOPIC = "performancing-addon-pffenable-topic";
  16. const PERFORMANCING_ADDON_PFFDISABLE_TOPIC = "performancing-addon-pffdisable-topic";
  17. const PERFORMANCING_ADDON_PFFTABCLICK_TOPIC = "performancing-addon-pfftabclick-topic";
  18.  
  19. function nsPerformancingService() {
  20.     this.wrappedJSObject = this;
  21.     this._bInited = false;
  22.     this.updateTimer = null;
  23.     this.checkTime = 600000; // to 10 mins
  24.     //this.checkTime = 60000; // 60 seconds (temp, for testing)
  25. }
  26.  
  27. nsPerformancingService.prototype = {
  28.     // ** START Public Functions **
  29.     init: function(aWebService) {
  30.         this.printLog("The Init Service ");
  31.         if (this._bInited) { //Check if we are already initialized
  32.             return true;
  33.         }
  34.         try{
  35.           //xmlhttprequest service
  36.           this.req = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Components.interfaces.nsIXMLHttpRequest);
  37.           
  38.           //Timer service
  39.           this._checkTimer = Components.classes["@mozilla.org/timer;1"];
  40.           this._checkTimer = this._checkTimer.createInstance(Components.interfaces.nsITimer);
  41.           //Observer Service
  42.           
  43.           //For launching a prompt from xpcom
  44.           this.promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService);
  45.           
  46.           //Get the last used window so we can call openDialog gBrowser, etc. from there.
  47.           this._windowManager = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
  48.           this._window = this._windowManager.getMostRecentWindow("navigator:browser"); //Get's the last browser window
  49.           
  50.           //Pref System
  51.           this.prefsService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
  52.           this.prefs = this.prefsService.getBranch("performancing.");// Get the "performancing." branch
  53.           
  54.           //Pref Observers
  55.           this.pbiPref = this.prefs.QueryInterface(Components.interfaces.nsIPrefBranchInternal);
  56.           this.pbiPref.addObserver("", this, false);//Pref Change Observer
  57.           
  58.           this._observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
  59.           
  60.         } catch (e){
  61.           this.printLog("Could note initialize Components. Error: " + e);
  62.           //Notify the UI there was an error
  63.         }
  64.         //this._observerService.addObserver(this, PERFORMANCING_ADDON_PFFSTART_TOPIC, false);
  65.         this._bInited = true;
  66.         //Do a one time call
  67.         this._checkTimer.initWithCallback(this, this.checkTime, Components.interfaces.nsITimer.TYPE_ONE_SHOT);// TYPE_ONE_SHOT | TYPE_REPEATING_SLACK
  68.         
  69.         return true;
  70.     },
  71.     
  72.     createTempObject: function(){
  73.         var doc = Components.classes["@mozilla.org/xul/xul-document;1"].createInstance(Components.interfaces.nsIDOMDocument);
  74.         var dude = doc.createElement("button");
  75.         dude.setAttribute("id", "temp-button");
  76.         dude.setAttribute("label", "Dude Man");
  77.         return dude;
  78.     },
  79.     
  80.     printLog: function(msg) {//Dump to -console.
  81.         dump("ScribeFire =>: " + msg +"\n");
  82.     },
  83.     
  84.     callNotification: function (aTopic, aData) {
  85.       this.performancingSendUpdateNotifications(aTopic, aData);
  86.     },
  87.     
  88.     onLastPFFClose: function () {
  89.       //Do something on browser close
  90.     },
  91.     
  92.     pffXmlHttpReq: {
  93.         request: Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Components.interfaces.nsIXMLHttpRequest),
  94.         
  95.         prepCall: function (aUrl, aType, aContent, aDoAuthBool, aUser, aPass) {
  96.             this.url = aUrl;
  97.             this.posttype = aType;
  98.             this.content = aContent;
  99.             this.username = aUser;
  100.             this.password = aPass;
  101.             this.doAuth = aDoAuthBool;
  102.             this.callback = aCallBackFunc;
  103.             if(this.doAuth){
  104.                 this.request.open(this.posttype, this.url, true, this.username, this.password);
  105.                 
  106.                 //Keeps stupid Authentication window from poping up
  107.                 this.request.channel.notificationCallbacks = {
  108.                     QueryInterface: function (iid) { // nsISupports
  109.                         if (iid.equals (Components.interfaces.nsISupports) ||
  110.                             iid.equals (Components.interfaces.nsIAuthPrompt) ||
  111.                             iid.equals (Components.interfaces.nsIInterfaceRequestor)) {
  112.                                 return this;
  113.                         }
  114.                         throw Components.results.NS_ERROR_NO_INTERFACE;
  115.                     },
  116.                     getInterface: function (iid, result) { // nsIInterfaceRequestor
  117.                         if (iid.equals (Components.interfaces.nsIAuthPrompt)) {
  118.                             return this;
  119.                         }
  120.                         Components.returnCode = Components.results.NS_ERROR_NO_INTERFACE;
  121.                         return null;
  122.                     },
  123.                     prompt: function (dialogTitle, text, passwordRealm, savePAssword, 
  124.                             defaultText, result) { // nsIAuthPromptInterface
  125.                         return false;
  126.                     },
  127.                     promptUsernameAndPassword: function (dialogTitle, text, 
  128.                             passwordRealm, savePassword, user, pwd) {
  129.                                   //Didn't work, asking for password again
  130.                         return false;
  131.                     },
  132.                     promptPassword: function (dialogTitle, text, passwordRealm, savePassword, user) {
  133.                         return false;
  134.                     }
  135.                 }
  136.             }else{
  137.                 this.request.open(this.posttype, this.url, true);
  138.             }
  139.             
  140.             if(this.posttype.toLowerCase() == "post"){
  141.                 this.request.setRequestHeader('Content-Length', this.content.length );
  142.             }
  143.             
  144.         },
  145.         
  146.         makeCall: function () {
  147.         this.request.setRequestHeader("User-Agent","Mozilla/5.0 (compatible; ScribeFire; http://www.scribefire.com/)");
  148.             this.request.send(this.content)
  149.         },
  150.         
  151.         requestComplete: function () {
  152.             if(theCall.request.readyState == 4){ 
  153.                 if (theCall.request.status == 200){ 
  154.                     theCall.onResult(theCall.request.responseText, theCall.request.responseXML); 
  155.                 }else{
  156.                     try{
  157.                         theCall.onError(theCall.request.statusMessage, theCall.request.responseXML);
  158.                     }catch(e){}
  159.                 } 
  160.             }
  161.         }
  162.     },
  163.     
  164.     
  165.     // ************************ START Private Functions ************************ **
  166.     
  167.     onAppClose: function () {
  168.       //Do something on browser close
  169.     },
  170.  
  171.     // nsISupports
  172.     QueryInterface: function (iid) {
  173.         if (!iid.equals(Components.interfaces.nsIPerformancingService) && !iid.equals(Components.interfaces.nsISupports) && !iid.equals(Components.interfaces.nsIAlertListener))
  174.                throw Components.results.NS_ERROR_NO_INTERFACE;
  175.  
  176.        return this;
  177.     },
  178.  
  179.     // nsITimerCallback
  180.     notify: function(aTimer) {
  181.       this.printLog("Timer Called");
  182.       var isTrue = true;
  183.       if(isTrue){
  184.           this.printLog("Start 10min Timer Call again");
  185.           this._checkTimer.initWithCallback(this, this.checkTime - 1000, Components.interfaces.nsITimer.TYPE_ONE_SHOT);// TYPE_ONE_SHOT | TYPE_REPEATING_SLACK
  186.       }else{
  187.           //Start small timer again
  188.           //this.printLog("Start small Timer Call again");
  189.           //this._checkTimer.initWithCallback(this, this.checkTime, Components.interfaces.nsITimer.TYPE_ONE_SHOT);// TYPE_ONE_SHOT | TYPE_REPEATING_SLACK
  190.       }
  191.     },
  192.     
  193.     //Windows Specific A'La MSN Messenger Notification Window
  194.     notifyWindows: function (numberNew) {
  195.         var title = "This is the Title"; //Needs to be localized
  196.         var msg = "This is the Message";//Needs to be localized
  197.       try {
  198.         this.alertService = Components.classes["@mozilla.org/alerts-service;1"].getService(Components.interfaces.nsIAlertsService);
  199.         //void showAlertNotification(in AString  imageUrl, in AString  title, in AString  text, in boolean  textClickable, in AString  cookie, in nsIObserver alertListener);
  200.         this.alertService.showAlertNotification("chrome://performancing/skin/scribefire-icon-32x32.png", title, msg, true, "", this);
  201.       }catch (e){}
  202.       this.playNotifySounds();
  203.     },
  204.     
  205.     //See: http://lxr.mozilla.org/aviary101branch/source/mail/components/prefwindow/content/pref-mailnews.js#162
  206.     playNotifySounds: function () {
  207.         try{
  208.           this.printLog("Play a Sound?!");
  209.           // sound notifications -Not Finished
  210.           var soundEnabled = false;
  211.           //soundEnabled = this.prefs.getBoolPref("soundnotif.enabled");
  212.           if (soundEnabled) {
  213.               this.printLog("Yes Sound is enabled!!");
  214.               var soundUrl =  this.prefs.getCharPref("soundnotif.uri");
  215.               var soundComponent = Components.classes["@mozilla.org/sound;1"].createInstance(Components.interfaces.nsISound);
  216.               if (soundUrl.indexOf("file://") == -1) {// Not on file system, so must be a system sound.
  217.                   this.printLog("Play a SYSTEM Sound?!");
  218.                   soundComponent.playSystemSound(soundUrl);
  219.               } else {
  220.                   this.printLog("Play a NORMAL Sound!");
  221.                   /*soundUrl = Components.classes["@mozilla.org/network/standard-url;1"].createInstance(Components.interfaces.nsIURL);
  222.                   soundUrl.spec = this.soundUri;
  223.                   soundComponent.play(soundUrl)*/
  224.                   var ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
  225.                   var soundUrl = ioService.newURI(soundUrl, null, null);
  226.                   soundComponent.play(soundUrl)
  227.               }
  228.            }
  229.         }catch(e){
  230.             this.printLog("Failed to play sound: "+ soundUrl);
  231.         }
  232.     },
  233.     
  234.     performancingSendUpdateNotifications: function (aTopic, aData) {
  235.         //Here we call the observers to update the UI
  236.         this.printLog("Do Notification aTopic: " + aTopic + " aData: " + aData);
  237.         this._observerService.notifyObservers(null,
  238.                                               aTopic,
  239.                                               aData);
  240.     },
  241.     
  242.     observe: function(aSubject, aTopic, aData) {
  243.         if(aTopic == "nsPref:changed"){//If Pref changes (performancing.*), do something
  244.              //this.printLog("A Pref has changed");
  245.              
  246.         }else if(aTopic == "xpcom-startup"){
  247.             this._observerService.addObserver(this, "quit-application", false);
  248.         }else if(aTopic == "quit-application"){
  249.             //Application is quitting baby
  250.             this.onAppClose();
  251.         }
  252.     }
  253.     // ** END Private Functions **
  254.     
  255. }
  256.  
  257. //********************* Module code *************************
  258.  
  259. var gPerformancingModule = {
  260.     CID: Components.ID("{f1ef9251-84a0-4d52-8e57-50e3c4a01a69}"),
  261.     contractID: "@performancing.com/performancing/PerformancingService;1",
  262.     className: "ScribeFire Service",
  263.  
  264.     firstTime: true,
  265.  
  266.     registerSelf: function (aCompMgr, aFileSpec, aLocation, aType) {
  267.         if (this.firstTime) {
  268.             this.firstTime = false;
  269.             throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
  270.         }
  271.     
  272.         aCompMgr = aCompMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  273.         aCompMgr.registerFactoryLocation(this.CID, this.className, this.contractID,
  274.                                        aFileSpec, aLocation, aType);
  275.     },
  276.     
  277.     unregisterSelf: function (aCompMgr, aFileSpec, aLocation) {
  278.     
  279.         aCompMgr = aCompMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  280.         aCompMgr.unregisterFactoryLocation(this.CID, this.className);
  281.     },
  282.     
  283.     getClassObject: function (aCompMgr, aCID, aIID) {
  284.         if (!aIID.equals(Components.interfaces.nsIFactory))
  285.             throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  286.  
  287.         if (!aCID.equals(this.CID)) 
  288.             throw Components.results.NS_ERROR_NO_INTERFACE;
  289.  
  290.         return this.factory;
  291.     },
  292.  
  293.     factory: {
  294.         createInstance: function (outer, iid) {
  295.             if (outer != null)
  296.                 throw Components.results.NS_ERROR_NO_AGGREGATION;
  297.  
  298.             return (new nsPerformancingService()).QueryInterface(iid);
  299.         }
  300.     },
  301.  
  302.     canUnload: function(compMgr) {
  303.       return true;
  304.     }
  305. };
  306.  
  307. function NSGetModule(compMgr, fileSpec) { 
  308.     return gPerformancingModule; 
  309. }
  310.